{include file="public/toper" /}
<script type="text/javascript" src="/static/js/myajax.js"></script>
<div class="x-nav">
    <span class="layui-breadcrumb">
        <a href="">商品管理</a>
        <a href="">商品列表</a>
    </span>
    <a class="layui-btn layui-btn-small" style="line-height:1.6em;margin-top:3px;float:right" href="javascript:location.replace(location.href);" title="刷新">
        <i class="layui-icon" style="line-height:30px">&#xe666;</i></a>
</div>
<div class="x-body">
    <div class="layui-row">
        <div class="layui-form layui-col-md12 x-so">
            <input type="text" name="sreach" value=""  style="width:250px;" id="key"   placeholder="请输入名称及其他一些商品参数" autocomplete="off" class="layui-input">
            <div class="layui-input-inline">
                <select name="store" id="storeId">
                    <option value="0">全部</option>
                    {volist name="store" id="vo"}
                    <option value="{$vo.store_id}">{$vo.name}</option>
                    {/volist}
                </select>
            </div>
            <div class="layui-input-inline">
                <select name="shelves" id="shelves">
                    <option value=".">全部</option>
                    <option value="1">上架</option>
                    <option value="0">下架</option>
                </select>
            </div>
            <button class="layui-btn" id="search"><i class="layui-icon">&#xe615;</i></button>
        </div>
    </div>
    <xblock class="demoTable">
        <!--<button class="layui-btn layui-btn-danger" data-type="getCheckData" onclick="delAll()"><i class="layui-icon"></i>批量删除</button>-->
        <button type="button" class="layui-btn layui-btn-primary" name="file" id="excel" lay-method="post" lay-type="file" lay-ext="xls|xlsx"><i class="layui-icon"></i>导入Excel</button>
        <button class="layui-btn" onclick="x_admin_show('添加商品','{:url(\'Goods/add\')}')"><i class="layui-icon"></i>添加</button>
    </xblock>
    <table class="layui-hide" id="test" lay-filter="test"></table>
</div>

<script type="text/html" id="image">
    <img src="{{d.thumbnail_img}}" class="thumb" thumb="{{ d.thumbnail_img }}"/>
</script>
<script type="text/html" id="sort">
    <input style="width: 100%" type="text" class="menu-sort j-ajax-input" name="sort" onkeyup="value=value.replace(/[^\d]/g,'')" value="{{d.sort}}" data-value="{{d.sort}}" data-href="{:url('sort')}?table=goods&ids={{d.goods_id}}">
</script>
<script type="text/html" id="statusTpl">
    <input type="checkbox" name="shelves" value="{{ d.shelves }}" lay-skin="switch" lay-filter="switchStatus" lay-text="上架|下架" {{ d.shelves == 1 ? 'checked' : '' }} data-href="{:url('state')}?table=goods&id={{ d.goods_id }}">
</script>
<script type="text/html" id="recommend">
    <input type="checkbox" name="recommend" value="{{ d.recommend }}" lay-skin="switch" lay-filter="switchStatus" lay-text="是|否" {{ d.recommend == 1 ? 'checked' : '' }} data-href="{:url('state')}?table=goods&type=1&id={{ d.goods_id }}">
</script>
<script type="text/html" id="action">
   <button class="layui-btn layui-btn layui-btn-xs"  onclick="x_admin_show('编辑商品','{:url(\'goods/edit\')}?id={{d.goods_id}}')"><i class="layui-icon">&#xe642;</i>编辑</button>
    <button class="layui-btn-danger layui-btn layui-btn-xs del_btn" id="{{d.goods_id}}" lay-event="del"><i class="layui-icon">&#xe640;</i>删除</button>
</script>
<script>
    layui.use(['table','upload'], function(){
        var table = layui.table, $ = layui.jquery;
        var layer = layui.layer;
        var loading = layer.load(1, {shade: [0.1,'#fff'] });
        var tableIn = table.render({
            method: 'post',
            elem: '#test'
            ,url: "{:url('Goods/index')}"
            ,id:'test'
            ,cols: [[
                {field:'goods_id', width:80, title: '序号',align: 'center',type:'numbers'}
                ,{field:'name', width:200, title: '名称',align: 'center'}
                ,{title: '缩略图',width:'150',align: 'center',templet: '#image'}
                ,{field: 'store_id',width:200, title: '门店',align: 'center', templet:function(d){ return d.store.name; }}
                ,{width:80,title: '排序',align: 'center',toolbar:'#sort'}
                ,{field:'method', width:180, title: '计件方式',align: 'center',templet: function(d){if(d.method==0){return '件';}else if(d.method==1){return '克重';}}}
                ,{field:'sale_type', width:180, title: '是否租赁',align: 'center',templet: function(d){if(d.sale_type==2){return '是';}else if(d.sale_type==1){return '不是';}}}
                ,{width: 140,title: '状态', align: 'center',templet: '#statusTpl'}
                ,{width:180,title: '是否推荐（夺宝）',align: 'center',toolbar:'#recommend'}
                ,{width: 190,title: '操作', align: 'center', toolbar: '#action'}
            ]]
            ,page: true
            ,limit: 30
            ,limits:[30,60,90,120,150,180,210,240,270,300]
        });
        layer.close(loading);


        var upload = layui.upload;
        upload.render({
            url: "{:url('Upload/upfile')}"
            ,elem:'#excel'
            ,accept:'file'
            ,exts:'xls|xlsx'
            ,before: function(input){
                var loading = layer.load(1, {shade: [0.1,'#fff'] });
            }
            ,done: function(res, index, upload){
                var path = res.path;
                $.post('{:url("Goods/import")}',{'path':path},function(data){
                    if(data.code == 200){
                        layer.close(loading);
                        layer.msg(data.msg, {icon: 1, time: 1000}, function(){
                            location.reload();
                        });
                    }else{
                        layer.close(loading);
                        layer.msg(data.msg, {icon: 2, anim: 6, time: 1000});
                    }
                });
            }
        });

        //搜索
        $('#search').on('click', function () {
            tableIn.reload({
                where: {key: $('#key').val(),sotre_id:$('#storeId').val(),shelves:$('#shelves').val()}
                ,page: {curr: 1}
            });
        });


        table.on('tool(test)', function(obj) {
            var data = obj.data;
            if(obj.event === 'del'){
                layer.confirm('您确定要删除【'+data.name+'】吗？',{btnAlign: 'c'}, function(index){
                    var loading = layer.load(1, {shade: [0.1,'#fff'] });
                    $.post("{:url('Goods/del')}",{goods_id:data.goods_id},function(res){
                        layer.close(loading);
                        if(res.code==200){
                            layer.msg(res.msg,{time:1000,icon:1});
                            tableIn.reload(test)
                        }else{
                            layer.msg(res.msg,{time:1000,icon:2});
                        }
                    });
                    layer.close(index);
                });
            }
        });
    });
</script>
{include file="public/footer"/}